<template>
  <!-- etka接收配件 -->
  <div>
    <TopFilter :list="topFilterList" :rows="2" :cols="3" :collapse="false" @filterChange="handleFilterChange"></TopFilter>
    <FilterTable
      ref="table"
      columnsRef="partPartPurchasePurchaseOrderMainEtkaReceivePart"
      :fetchapi="fetchapi"
      :params="fetchParams"
      :columns="columns"
      :datakey="'rows'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="handleRowSelectChange"
    >
      <template slot="controls">
        <el-button size="small" type="primary" @click="handleChoseNowOrder">添加现有订单</el-button>
        <el-button size="small" type="primary" @click="handleCreateNewOrder">创建新订单</el-button>
        <el-button size="small" type="primary" @click="handleDelete">删除</el-button>
      </template>
    </FilterTable>
    <div class="bottomBtnWrap">
      <el-button @click="handleClose">取 消</el-button>
      <!-- <el-button @click="handleConfirm" type="primary">确 定</el-button> -->
    </div>

    <!-- 添加现有订单选择弹窗 -->
    <BaseDialog :visible.sync="choseOrderVisible" title="选择订单" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <etkaPartChsoeOrder @close="choseOrderVisible = false" @onConfirm="handleOrderSelectConfirm" />
    </BaseDialog>
  </div>
</template>

<script>
import { messageAction, notifyAction } from '@/utils';

import { queryEtkaParts, deleteEtkaParts } from '@/api/part/partPurchase/purchaseOrder';

import etkaPartChsoeOrder from './etkaPartChsoeOrder';
import { dictionary } from '@/utils/dictMixin';

export default {
  components: {
    etkaPartChsoeOrder
  },
  mixins: [dictionary],
  data() {
    return {
      topFilterList: this.createTopFilterList(),
      columns: this.createTableColumns(),
      choseOrderVisible: false,
      fetchapi: queryEtkaParts,
      fetchParams: { xhrAbort: false },
      selectedRowData: [] // 选中的列表数据
    };
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'RANGE_DATE',
          label: '需求日期',
          fieldName: 'orderDateBefore|orderDateAfter',
          dateFormat: 'yyyy-MM-dd'
        },
        {
          type: 'INPUT',
          label: '配件代码',
          fieldName: 'partNo'
        },
        {
          type: 'CHECKBOX',
          label: 'FIB',
          fieldName: 'fib',
          options: {
            trueValue: '10041001',
            falseValue: '10041002'
          },
          initialValue: '10041002'
        },
        {
          type: 'INPUT',
          label: '委托书编号',
          fieldName: 'roNo'
        },
        {
          type: 'INPUT',
          label: '底盘号',
          fieldName: 'vin'
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '委托书编号',
          dataIndex: 'roNo',
          filter: true,
          filterType: 'input'
        },
        {
          title: '需求日期',
          dataIndex: 'dDemand',
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '底盘号',
          dataIndex: 'vVin',
          filter: true,
          filterType: 'input'
        },
        {
          title: '配件代吗',
          dataIndex: 'partNo',
          width: 150,
          showOverflowTooltip: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配件名称',
          dataIndex: 'partName',
          width: 150,
          showOverflowTooltip: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '需求数量',
          dataIndex: 'nOrderQty',
          filter: true,
          filterType: 'number'
        },
        {
          title: 'FIB',
          dataIndex: 'isFib',
          filter: true,
          filterType: 'input',
          dictItems: this.createDictList(1004)
        }
      ];
    },
    // 点击搜索
    handleFilterChange(e) {
      this.fetchParams.xhrAbort = true;
      this.fetchParams = Object.assign(this.fetchParams, {
        xhrAbort: false,
        ...e
      });
    },
    // table选择行项变化
    handleRowSelectChange(e) {
      this.selectedRowData = e;
    },
    // 点击添加现有订单
    handleChoseNowOrder() {
      if (this.selectedRowData.length) {
        this.choseOrderVisible = true;
      }
    },
    // 点击新建订单
    handleCreateNewOrder() {
      if (this.selectedRowData.length) {
        let fibData = this.topFilterList.find(item => item.fieldName == 'fib');
        this.selectedRowData.map(item => {
          item.cEtka = 1;
          item.vSvcBillNo = item.roNo;
        });
        this.$emit('onEtkaPartCreateNewOrder', {
          isFib: !!fibData.initialValue,
          partList: this.selectedRowData
        });
        this.handleClose();
      } else {
        messageAction('请选择需要添加的配件', 'warning');
      }
    },
    // 添加现有订单点击确定
    handleOrderSelectConfirm(e) {
      if (this.selectedRowData.length) {
        this.selectedRowData.map(item => {
          item.cEtka = 1;
          item.vSvcBillNo = item.roNo;
        });
        this.$emit('onEtkaPartAddToOldOrder', {
          orderInfo: e,
          partList: this.selectedRowData
        });
        this.handleClose();
      } else {
        messageAction('请选择需要添加的配件', 'warning');
      }
    },
    // 点击删除
    async handleDelete() {
      if (this.selectedRowData.length) {
        let itemIds = this.selectedRowData.map(item => item.itemId);
        let res = await deleteEtkaParts({
          itemId: itemIds.join(',')
        });
        if (res.resultCode == 200) {
          console.log('删除成功');
          notifyAction('删除成功', 'success');
          this.$refs.table.EXECUTE_DELETE(this.selectedRowData);
        }
      } else {
        notifyAction('请选择至少一条数据', 'warning');
      }
    },
    handleClose() {
      this.$emit('close');
    }
  }
};
</script>

<style scoped>
.bottomBtnWrap {
  position: absolute;
  left: 0px;
  bottom: 0px;
  right: 0px;
  z-index: 9;
  border-top: 1px solid rgb(233, 233, 233);
  padding: 10px 20px;
  background: rgb(255, 255, 255);
  text-align: right;
}
</style>
